package com.tiny.work.leetcode;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class BinaryTreeOrder {
    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            inOrder(root, result);
            return result;
        }

        public void inOrder(TreeNode root, List<Integer> result) {
            if (root == null) {
                return;
            }
            inOrder(root.left, result);
            result.add(root.val);
            inOrder(root.right, result);
        }
    }

    public int index;

    public TreeNode creataTree(List<Integer> nodes) {
        if (nodes == null || nodes.size() == 0 || index >= nodes.size()) {
            return null;
        }
        Integer value = nodes.get(index++);
        if (value == null){
            return null;
        }
        TreeNode root = new TreeNode(value);
        root.left = creataTree(nodes);
        root.right = creataTree(nodes);
        return root;
    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return  null;
        }
        invertTree(root.left);
        invertTree(root.right);
        TreeNode temp = root.left;
        root.left = root.right;
        root.right =temp;
        return root;
    }

    @Test
    public void test() {
        List<Integer> nodes = new ArrayList<Integer>() {{
            add(1);
            add(null);
            add(2);
            add(3);
        }};
        TreeNode root = new BinaryTreeOrder().creataTree(nodes);
        Solution solution = new Solution();
        List<Integer> result = solution.inorderTraversal(root);
        System.out.print(result);
        new BinaryTreeOrder().invertTree(root);
    }
}


